<?php

namespace app\admin\controller;

use org\Jdk_inc;
use think\Controller;
use think\Db;

error_reporting(0);
header("Access-Control-Allow-Origin:*");
class Share extends Controller
{
    
    //获取微信菜单
    public function get_weixin_meum(){
        
        $wx = new Jdk_inc();
        $token_access = $wx->getToken();

        $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={$token_access}";
        $json = file_get_contents($url);

        $result = json_decode($json, true);
        if($result['errcode']){
            echo json_encode($result);return;
        }
        return json(['errcode' => 0, 'data' => $result]);
    }  

   ///创建菜单
    public function createmenu()
    {
        $menu = $_GET['menu'];
        //更换成自己的APPID和APPSECRET
        $wx = new Jdk_inc();
        $ACC_TOKEN = $wx->getToken();
        $MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $ACC_TOKEN;
        $ch = curl_init($MENU_URL);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $menu);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($menu)));
        $info = curl_exec($ch);
        $menu = json_decode($info, true);

        return json($menu);
          
    }
    //获取素材 material_detail
    public function material_detail()
    {
        $param = input("param.");
        $media_id = isset($param['media_id']) && !empty($param['media_id']) ? trim($param['media_id']) : null;
      
        if (is_null($media_id)) {
            return;
        }

        $wx = new Jdk_inc();
        $json = json_encode(['media_id' => $media_id]);
        $ACC_TOKEN = $wx->getToken();
        $url = "http://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" . $ACC_TOKEN;
        $result = httpPost($url, $json);
        $result = json_decode($result, true);
        $arr = [
            'errcode' => 0,
            'data' => $result,
        ];
        return $arr;
    }

}
